/**
 * 渠道统计管理初始化
 */
var ChannelPromoteInfo = {
    id: "ChannelPromoteInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    isAdmin: false
};

/**
 * 初始化表格的列
 */
ChannelPromoteInfo.initColumn = function () {
    return [[
        {title: '渠道名称', field: 'channelName', align: 'center',width: 200},
        {title: '应用名称', field: 'appName', align: 'center'},
        {title: 'UUID', field: 'deviceUUID', align: 'center'},
        {title: '签包时间', field: 'signTime', align: 'center',width: 200}
    ]];
};

ChannelPromoteInfo.adminColumn = function () {
    return [[
        {title: '商户名称', field: 'merchantName', align: 'center',width: 150},
        {title: '应用名称', field: 'appName', align: 'center'},
        {title: '渠道名称', field: 'channelName', align: 'center',width: 200},
        {title: 'UUID', field: 'deviceUUID', align: 'center'},
        {title: '签包时间', field: 'signTime', align: 'center',width: 200}
    ]];
};

ChannelPromoteInfo.tableList = function (queryData) {
    layui.use("table", function () {
        var table = layui.table;

        ChannelPromoteInfo.table = table;

        table.render({
            elem: "#table",
            url: Feng.ctxPath + "/channelPromoteInfo/list",
            cellMinWidth: 100,
            where: queryData,
            page: true,
            limit: 20,
            cols: ChannelPromoteInfo.isAdmin?ChannelPromoteInfo.adminColumn():ChannelPromoteInfo.initColumn(),
            parseData: function (res) {
                return {
                    "code": 0,
                    "msg": "获取成功",
                    "count": res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        });
    });
};

/**
 * 获取渠道签包设备的图表数据
 */
ChannelPromoteInfo.getDeviceInfoOfBar = function(){
    var queryData = {};
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    let ajax = new $ax(Feng.ctxPath + "/channelPromoteInfo/deviceBar", function (data) {
        if (!$.trim(data.namesArr)){
            Feng.error("没有查询到所需内容")
        }
        ChannelPromoteInfo.showDeviceBar(data);
    }, function (data) {
        Feng.error("获取信息失败!"+data.responseJSON.message);
    });
    ajax.set(queryData);
    ajax.start();
}

/**
 * 展示渠道签包设备图表
 * @param deviceInfo
 */
ChannelPromoteInfo.showDeviceBar = function(deviceInfo){
    const dom = document.getElementById("deviceInfo");
    const myChart = echarts.init(dom);
    const option = {
        title: {
            text: '渠道签包设备统计',
            left: 'center'
        },
        yAxis: {
            type: 'value'
        },
        xAxis: {
            type: 'category',
            data: deviceInfo.namesArr,
            axisLabel:{
                interval: 0,
                rotate: -45
            }
        },
        series: [
            {
                type: 'bar',
                data: deviceInfo.countArr,
                itemStyle: {//系列级个性化
                    normal: {
                        color: "#2d8ffd",
                        label:{
                            show:true,
                            position : 'top',
                            formatter : function (params) {
                                return params.value
                            },
                            textStyle:{
                                fontSize:14
                            }
                        },
                    }
                },
                barWidth: 30
            }
        ]
    };

    if (option && typeof option === "object") {
        myChart.setOption(option, true);
    }
}

$(function () {
    if ($("#isAdmin").val() == "超级管理员"){
        ChannelPromoteInfo.isAdmin = true;
    }else{
        ChannelPromoteInfo.isAdmin = false;
    }
    ChannelPromoteInfo.tableList();

    ChannelPromoteInfo.getDeviceInfoOfBar();
});
